package com.tf_cut.backend.seller.mobile.controller;

import com.github.pagehelper.PageHelper;
import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.person.vo.PersonAndAccountVo;
import com.tf_cut.backend.seller.enums.ProjectPersonReceiveStatus;
import com.tf_cut.backend.seller.service.*;
import com.tf_cut.backend.seller.vo.*;
import com.tf_cut.backend.util.SerialNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/backend/seller/mobile")
public class SellerMobileController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private SellerProjectService sellerProjectService;

    @Autowired
    private SellerBillTypeService sellerBillTypeService;

    @Autowired
    private SellerBillService sellerBillService;

    @Autowired
    private SellerConsigneeService sellerConsigneeService;

    @Autowired
    private SellerProjectPersonService sellerProjectPersonService;

    @Autowired
    private SerialNoUtil serialNoUtil;

    @GetMapping("/seller/get")
    public Result<SellerAndAccountVo> get(@RequestParam  String loginId) {
        List<SellerAndAccountVo> data = sellerService.list(SellerAndAccountVo.builder().loginId(loginId).build()).getData();
        if(data==null||data.isEmpty()){
            return Result.of(null,false,"数据错误");
        }else {
            return Result.of(data.get(0));
        }

    }

    @PostMapping("/seller/insert")
    public Result<SellerAndAccountVo> insertSellerAndAccount(@RequestBody  SellerAndAccountVo  avo) {
        Result<SellerAndAccountVo> result = null;
        try {
            result = sellerService.insertSellerAndAccount(avo);
        } catch(Exception e) {
            log.error(e.getMessage(),e);
            result = Result.of(avo);
            result.setMsg(e.getMessage());
            result.setFlag(false);
        }
        return result;
    }

    @PostMapping("/seller/updateInfo")
    public Result<SellerAndAccountVo> updateInfo(@RequestBody SellerAndAccountVo svo) {

        Result<SellerAndAccountVo> result = sellerService.save(svo);
        return result;
    }


    @PostMapping("/project/insert")
    public Result<SellerProjectVo> insertproject(@RequestBody SellerProjectVo svo) {

        //整项目code

        svo.setProjectCode(serialNoUtil.genSerialNo("X", svo.getPlatform(), svo.getMerchantNumber()));

        svo.setCreatedTime(new Date());
        svo.setStatus("Y");
        Result<SellerProjectVo> result = sellerProjectService.save(svo);
        return result;
    }

    @PostMapping("/projectPerson/insert")
    public Result<SellerProjectPersonVo> projectPerson(@RequestBody SellerProjectPersonVo svo) {
        svo.setCreatedTime(new Date());
       svo.setUpdatedTime(new Date());
       svo.setStatus(1);
        Result<SellerProjectPersonVo> result = sellerProjectPersonService.save(svo);
        return result;
    }

    @GetMapping("/project/fingPageProject")
    public com.github.pagehelper.PageInfo<SellerProjectVo>  fingPageProject(@RequestParam(value = "projectName",required = false)String projectName,
                                                                          @RequestParam(value = "startDate",required = false) String startDate,
                                                                          @RequestParam(value = "endDate",required = false) String endDate,
                                                                          @RequestParam(value = "havePersonInfo",required = false) String havePersonInfo,
                                                                          @RequestParam(value = "personInfoId",required = false) String personInfoId,
                                                         @RequestParam(value = "pageNum",required = false) Integer pageNum,
                                                         @RequestParam(value = "pageSize",required = false) Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        Integer havePersonInfoInteger=null;
        if(StringUtils.isNotBlank(havePersonInfo)){
            havePersonInfoInteger=Integer.parseInt(havePersonInfo.trim());
        }


        List<SellerProjectVo> projectvos = this.sellerProjectService.findMobileProject("".equals(projectName)?null:projectName,
                                                                                "".equals(startDate)?null:startDate,
                                                        ( "".equals(endDate)||endDate==null)?null:endDate+"  23:59:59",
                                                                                havePersonInfoInteger,
                                                                                 "".equals(personInfoId)?null:personInfoId);

        return new com.github.pagehelper.PageInfo<SellerProjectVo>(projectvos);
    }

    @GetMapping("/project/view")
    public Result<SellerProjectVo> projectview(
            @RequestParam(value = "projectId",required = false) String projectId,
            @RequestParam(value = "personInfoId",required = false) String personInfoId) {

        Result<SellerProjectVo> result = sellerProjectService.get(projectId);

        Result<List<SellerProjectPersonVo>> list = this.sellerProjectPersonService.list(SellerProjectPersonVo.builder()

                .projectId(projectId).personInfoId(personInfoId).build());

        if(list.getData().size()>0){
            //,1 未承接  2 承接
            result.getData().setHavePersonInfo(2);
        }else {
            result.getData().setHavePersonInfo(1);
        }

        return result;
    }



    //新增根据供应商查询
    @GetMapping("/project/findBySellerId")
    public List<SellerProjectVo> findBySellerId(
                                             @RequestParam(value = "projectName",required = false)String projectName,
                                             @RequestParam(value = "startDate",required = false) String startDate,
                                             @RequestParam(value = "endDate",required = false) String endDate,
                                             @RequestParam(value = "sellerId",required = false) String sellerId) {

        SellerProjectVo.SellerProjectVoBuilder builder = SellerProjectVo.builder().sellerId(sellerId);
        if(StringUtils.isNotBlank(projectName)){
            builder.projectName(projectName);
        }
        if(StringUtils.isNotBlank(startDate)){
            builder.startTime(startDate);
        }
        if(StringUtils.isNotBlank(endDate)){
            builder.endTime(endDate+"  23:59:59");
        }
        List<SellerProjectVo> projectByVo = this.sellerProjectService.findProjectByVo(builder.build());
        return projectByVo;
    }

    @GetMapping("/project/findProjectPersonInfoByProjectId")
    public Result<SellerProjectPersonInfoVo> findProjectPersonInfoByProjectId(
            @RequestParam(value = "projectId",required = false) String projectId) {
        SellerProjectVo data =sellerProjectService.findByProjectCodeOne(projectId);
        List<PersonAndAccountVo> vos=sellerProjectService.findProjectPersonInfoByProjectId(projectId);
        SellerProjectPersonInfoVo build = SellerProjectPersonInfoVo.builder().personAndAccountVos(vos).sellerProjectVo(data).build();
        return Result.of(build);
    }





    @GetMapping("/billtype/list")
    public Result<List<SellerBillTypeVo>> billTypeList() {

        List<SellerBillTypeVo> projectvos = this.sellerBillTypeService.list(SellerBillTypeVo.builder().build())
                .getData();
        Result<List<SellerBillTypeVo>> result = Result.of(projectvos);
        return result;
    }

    @PostMapping("/bill/insert")
    public Result<SellerBillAndConsigneeVo> insertBill(@RequestBody SellerBillAndConsigneeVo svo) {
        svo.setCreateTime(new Date());
       svo.setStatus(0);//0：待确认；1：已确认；2：已驳回
       svo.setOrderSource(1);//0：个独订单；1：商户订单
        Result<SellerBillAndConsigneeVo> result = sellerBillService.insertBillAndConsignee(svo);
        return result;
    }

    private void setPersonReceiveProjectStatus(List<SellerProjectVo> projectVos, String personInfoId) {
        projectVos.forEach(sellerProjectVo -> {
            String projectPerson = sellerProjectVo.getPersonInfoId();
            boolean bothPersonIdNotNull = StringUtils.isNotBlank(projectPerson) && StringUtils.isNotBlank(personInfoId);
            if (bothPersonIdNotNull && personInfoId.equals(projectPerson)) {
                sellerProjectVo.setProjectReceiveStatus(ProjectPersonReceiveStatus.RECEIVED.getValue());
            } else {
                sellerProjectVo.setProjectReceiveStatus(ProjectPersonReceiveStatus.UNRECEIVED.getValue());
            }
        });
    }
    
    @GetMapping("/project/get")
    public List<SellerProjectVo> fingProject(@RequestParam("personName") String personName,
                                             @RequestParam("sellerName") String sellerName,
                                             @RequestParam("projectName")String projectName,
                                             @RequestParam("startDate") String startDate,
                                             @RequestParam("endDate") String endDate) {

        List<SellerProjectVo> projectvos = this.sellerProjectService.findProjects("".equals(personName)?null:personName,
                                                                                "".equals(sellerName)?null:sellerName,
                                                                                "".equals(projectName)?null:projectName,
                                                                                "".equals(startDate)?null:startDate,
                                                                                "".equals(endDate)?null:endDate+"  23:59:59");

        return projectvos;
    }

}
